#ifndef SCANNER_H
#define SCANNER_H

#define MESSAGE_TAB		1


#include "sane.h"
#include "saneopts.h"
#include "scene.h"
#include "config.h"
#include "widget.h"
#include "sanedeviceoption.h"
//#include "image.h"

#include <QClipboard>
#include <QLabel>
#include <QLineEdit>



namespace Ui {
class Scanner;
}

class Scanner : public QDialog
{
    Q_OBJECT

private:
    Ui::Scanner *ui;
    QSettings*	m_settings;
    bool m_isScanning;

    bool isScanning () { return m_isScanning; }
    void setScanning (bool b) { m_isScanning = b; }

    bool m_saneOK;

    void initSettings ();
    void writeSettings();
    bool m_showOnlyFirstGroup;

    QString m_fileName;
    QImage m_imageFromScanner;
    bool m_previewMode;

    ImagePart* m_part;

    QProcess *m_process;
    const SANE_Device** m_devices [50];
    const SANE_Device* m_currentDevice;
    bool m_isHPdevice;
    bool isHPdevice () { return m_isHPdevice; }

    bool initSane ();
    SANE_Handle m_currentHandle;
    QList<SaneOption*> m_saneOptions;
    void getAllSaneOptions ();
    SANE_Status verifyStatus (SANE_Status status, QString info = QString ());

    QStringList m_modes;
    int m_resolutionIndex;
    int m_modeIndex;

    QStringList m_messages;

    QGraphicsRectItem* m_selectionRect;
    QGraphicsPixmapItem* m_pixmapItem;
    void sendMessage(const QString& msg);
    QToolBox *m_toolBox;
    void clearPreview ()
    {
        m_pixmapItem->setPixmap (QPixmap ());
        QApplication::processEvents ();
    }
    void readSettings ();

    QString strValue (const QString& optionName);

    void disableAllOptions ();
    void enableActiveOptions () { foreach (SaneOption* d, m_saneOptions) d->setEnabledIfActive (); }
    void resetHandle (SANE_Handle h) { foreach (SaneOption* d, m_saneOptions) d->setHandle (h); }

    QHash<QString, int> foundDevices ();

    bool saveImage (const QImage &image);
    void clearToolBox();

private slots:
    void onDeviceChanged (const int listIndex);
    void displayMessage (const QString& msg);
    void on_copyMessages_clicked();
    void on_tabWidget_currentChanged (const int index);
    void reloadAllOptions ();
    void on_acquire_clicked ();
    void onProcessFinished (const int result, const QProcess::ExitStatus status);
    void onProcessError (QProcess::ProcessError error);
    void onProcessRRSE();
    void on_Ok_clicked();
    void on_cancel_clicked();
    void on_removeMargins_toggled(const bool checked);
    void onImageIsAvailable ();

public:
    explicit Scanner (const QString &fileName, CenteredWidget *parent = 0);
    ~Scanner();

    void closeEvent (QCloseEvent* event) { if (isScanning ()) event->ignore (); else event->accept (); }

signals:
    void message (const QString& err);
    void imageIsAvailable ();
};

#endif // SCANNER_H
